/*kvan
 *2014.5.12
 */
package com.taxsystem.likaifan.taxcompute;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class VehicleTaxCompute extends Activity {

	public final static String RECI_COAST = "sw.system.HTTUTIL";


	final int MY_NOTICE = 0x111;
	final int NAV_SETTING = 0x112;
	final int ACCOUNT_SETTING = 0x113;
	final int INDIVIDUAL_SETTING = 0x114;
	private EditText total_weight;;
	private TableRow weight_row;
	private TableRow displacement_row;
	private short unit_type_flag;
	
    private String result ="{\"data\":{\"tax\": \"199\"}}";
	
	public static String carType =
	"{"
	+ "\"data\": {"
	+ "\"carType\": ["
	+ "{"
	+ "\"code\": \"XJC\", "
	+ "\"name\": \"小轿车\", "
	+ "\"unit\": \"pl\", "
	+ "\"unitTips\": \"每辆\""
	+ "},"
	+ "{"
	+ "\"code\": \"ZKC\", "
	+ "\"name\": \"中型客车(核定载客10-19人)\", "
	+ "\"unit\": \"carType\", "
	+ "\"unitTips\": \"每辆\""
	+ "},{\"code\": \"DKC\",\"name\": \"大型客车(核定载客10-19人)\", \"unit\": \"carType\",\"unitTips\": \"每辆\"},{\"code\": \"HC\",\"name\": \"货车\","
	+ "\"unit\": \"curbWeight\",\"unitTips\": \"整备质量每吨\"},{\"code\": \"GC\",\"name\": \"挂车\",\"unit\": \"curbWeight\",\"unitTips\": \"整备质量每吨\""
	+ "},{\"code\": \"MTC\",\"name\": \"摩托车\",\"unit\": \"carType\",\"unitTips\": \"每辆\"}],"
	+ "\"pl\": [{\"code\": \"PL0010\",\"name\": \"1.0升(含)以下\"},"
	+ "{\"code\": \"PL1016\",\"name\": \"1.0升以上至1.6升(含)\"},{\"code\": \"PL1620\",\"name\": \"1.6升以上至2.0升(含)\"},"
	+ "{\"code\": \"PL2025\",\"name\": \"2.0升以上至2.5升(含)\"},{\"code\": \"PL2530\",\"name\": \"2.5升以上至3.0升(含)\"},"
	+ "{\"code\": \"PL3040\",\"name\": \"3.0升以上至4.0升(含)\"},{\"code\": \"PL4000\",\"name\": \"4.0升以上\"}]}}"
	;

	
    private static String[] type = null;// = {"小轿车","卡车","客车"};
    private static String[] displacement;// = {"1L","2L","3L"};
    
    
	private Spinner vehicle_type;
	private Spinner vehicle_displacement;

	private String vehicle_type_str;
	private String vehicle_displacement_str;
	
	private String[] car_type_code;
	private String[] pl_code;
	
	private Button button;
	public String[] unitType;
	
	private String requestUrl = "http://*.app/calculator/getcartypes";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.vehicle_tax_compute);
		weight_row = (TableRow) findViewById(R.id.weight_row);
		displacement_row = (TableRow) findViewById(R.id.displacement_row);
		total_weight = (EditText) findViewById(R.id.total_weight);
		
		vehicle_type = (Spinner) findViewById(R.id.vehicle_type);
		vehicle_displacement = (Spinner) findViewById(R.id.vehicle_displacement);
	
		// 注册广播
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		
		Thread thread = new Thread() 
		{
			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
					//获取服务器返回的信息
				/*
					String result =  null;

					try {
						result = com.taxsystem.tool.HttUtil
								.getRequest(requestUrl);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					*/
					String result =VehicleTaxCompute.carType;

					intent.putExtra("result", result);
					//发送广播
					sendBroadcast(intent);
			}

		};
		thread.start();
	}

	
	public void check(View view)
	{
		if(unit_type_flag == 1)
		{
			String url = "/app/calculator/cartax?cartype="+vehicle_type_str+"&pl="+vehicle_displacement_str;
	    	//String reslut = HttUtil.getRequest(requestUrl);
			Intent intent = new Intent(this, VehicleTaxResult.class);
			intent.putExtra("result", result);
			startActivity(intent);
		}
		if(unit_type_flag == 2)
		{
			String url = "/app/calculator/cartax?cartype="+vehicle_type_str;
	    	//String reslut = HttUtil.getRequest(requestUrl);
			Intent intent = new Intent(this, VehicleTaxResult.class);
			intent.putExtra("result", result);
			startActivity(intent);
		}
		if(unit_type_flag == 3)
		{
			String url = "/app/calculator/cartax?cartype="+vehicle_type_str+"&weight="+ total_weight.getText();
	    	//String reslut = HttUtil.getRequest(requestUrl);
			Intent intent = new Intent(this, VehicleTaxResult.class);
			intent.putExtra("result", result);
			startActivity(intent);
		}

		 
	}
	
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
				//获取JSONObject对象
			try {
				JSONObject result = new JSONObject(
				intent.getStringExtra("result"));
				JSONObject data = new JSONObject(
						result.getString("data"));

				JSONArray carType = data.getJSONArray("carType");
				JSONArray pl = data.getJSONArray("pl");
				type = new String[carType.length()];
				car_type_code = new String[carType.length()];
				
				unitType = new String[carType.length()];
				
				for(int i=0; i<carType.length(); i++)
				{
					type[i] = carType.getJSONObject(i).getString("name");
					unitType[i] = carType.getJSONObject(i).getString("unit");
					car_type_code[i] = carType.getJSONObject(i).getString("code");
				}
				
				vehicle_type.setPrompt("请选择收入类型"); 
				ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(VehicleTaxCompute.this, android.R.layout.simple_spinner_item, type);
			    type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
			    vehicle_type.setAdapter(type_adapter);  

			    vehicle_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			           {
			        	   vehicle_type_str = car_type_code[arg2];
			        	   vehicle_displacement_str = pl_code[arg2];
			        	   
			        	   if(unitType[arg2].equals("pl"))
			        	   {
			        		   
			        		   unit_type_flag = 1;
			        		   displacement_row.setVisibility(0);
			        		   weight_row.setVisibility(8);
			        	   }
			        	   if(unitType[arg2].equals("carType"))
			        	   {
			        		   unit_type_flag = 2;
			        		   displacement_row.setVisibility(8);
			        		   weight_row.setVisibility(8);
			        	   }
			        	   if(unitType[arg2].equals("curbWeight"))
			        	   {
			        		   unit_type_flag =3;
			        		   displacement_row.setVisibility(8);
			        		   weight_row.setVisibility(0);
			        	   }
			        	   
			        	   
			        	   arg0.setVisibility(View.VISIBLE);
			           }

			           public void onNothingSelected(AdapterView<?> arg0)
			           {
			           }

			       });

			    
			    displacement = new String[pl.length()];
			    pl_code = new String[pl.length()];
				for(int i=0; i<pl.length(); i++)
				{
					pl_code[i] =  pl.getJSONObject(i).getString("code");
					displacement[i] = pl.getJSONObject(i).getString("name");
				}

			    vehicle_displacement.setPrompt("请选择收入类型"); 
				ArrayAdapter<String> displacement_adapter = new ArrayAdapter<String>(VehicleTaxCompute.this, android.R.layout.simple_spinner_item, displacement);
				displacement_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
			    vehicle_displacement.setAdapter(displacement_adapter);  

			    vehicle_displacement.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			           {
			        	   vehicle_displacement_str = displacement[arg2];
			        	   arg0.setVisibility(View.VISIBLE);
			           }

			           public void onNothingSelected(AdapterView<?> arg0)
			           {
			           }

			       });
						
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_mian, menu);
		menu.add(0, MY_NOTICE, 0, "我的通知");
		menu.add(0, NAV_SETTING, 0, "导航设置");
		menu.add(0, ACCOUNT_SETTING, 0, "账号设置");
		menu.add(0, INDIVIDUAL_SETTING, 0, "个性化设置");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		
		
		//判断单击的是哪个菜单项，并针对性的作出响应。
		switch (mi.getItemId())
		{
			case android.R.id.home:
				this.finish();
				break;
			case MY_NOTICE:
				Log.d("hehe","heh");
				Toast toast1 = Toast.makeText(getApplicationContext(),
					     "MY_NOTICE", Toast.LENGTH_SHORT);
				toast1.show();
				break;
			case NAV_SETTING:
				Toast toast2 = Toast.makeText(getApplicationContext(),
					     "NAV_SETTING", Toast.LENGTH_SHORT);
				toast2.show();;
				break;
			case ACCOUNT_SETTING:
				Toast toast3 = Toast.makeText(getApplicationContext(),
					     "ACCOUNT_SETTING", Toast.LENGTH_SHORT);
				toast3.show();
				break;
			case INDIVIDUAL_SETTING:
				Toast toast4 = Toast.makeText(getApplicationContext(),
					     "INDIVIDUAL_SETTING", Toast.LENGTH_SHORT);
				toast4.show();
				break;

		}
		return true;
	}

}
